
class ListNode:
    def __init__(self,value,next = None):
        if isinstance(value,int): # 判断传入的是不是一个int型的数
            self.value = value
            self.next = next

        elif isinstance(value,list): #如果为列表
            self.value = value[0]
            self.next = next
            head = self
            for i in range(1,len(value)):
                node = ListNode(value[i])
                head.next = node
                head = head.next


class Solution:
    def reverseList(self, head:ListNode) -> ListNode:
        perv = None  # 定义一个空指针
        curr = head  # 记录当前链表

        while curr != None:
            next1 = curr.next  # 记录下一个节点的值
            curr.next = perv   # 把当前链表的指针指向perv空指针，改变节点的指向
            perv = curr
            curr = next1 # 继续处理后面的链表

        return perv  #返回翻转后的表头


#创建链表
lst = ListNode([1,2,3,4,5,6])


s1 = Solution()
re_list = s1.reverseList(lst)
#输出反转后的链表
while re_list:
    print(re_list.value,end=' ')
    re_list = re_list.next
